
#include <vector_types.h>
#include <math_functions.h>

struct Particula {
    float3 velocidade;
    float massaEspecifica;
    float pressao;
    float3 forcaPressao;
    float3 forcaVisc;
    float3 forcaAceleracao;
};

__global__ void PressaoKernel(float3* posicoesParticulasT, float3* forcaPressao, int numeroParticulas, int resolucaoGridX, int resolucaoGridY, unsigned int* values, float deltaXGrid, float deltaYGrid, unsigned int* offSet, unsigned int* count, float h, Particula* particulasT, float massa) {
    int thread = threadIdx.x;
    int block = blockIdx.x;
    int dim = blockDim.x;
    int indiceValue = dim * block + thread;
    int jAtual = 0;
    int iAtual = 0;
    int jMin = 0;
    int jMax = resolucaoGridX;
    int iMin = 0;
    int iMax = resolucaoGridY;
    int idCell;
    int kInicial;
    int kFinal;

    if (indiceValue < numeroParticulas) {
        int indiceParticulaAtual = values[indiceValue];
        particulasT[indiceParticulaAtual].forcaPressao.x = 0;
        particulasT[indiceParticulaAtual].forcaPressao.y = 0;
        particulasT[indiceParticulaAtual].forcaPressao.z = 0;
        //forcaPressao[indiceParticulaAtual].x = 0;
        //forcaPressao[indiceParticulaAtual].y = 0;
        //forcaPressao[indiceParticulaAtual].z = 0;
        jAtual = (posicoesParticulasT[indiceParticulaAtual].x / deltaXGrid);
        iAtual = (posicoesParticulasT[indiceParticulaAtual].y / deltaYGrid);

        //VERIFICAO DAS EXTREMIDADES.
        if (jAtual <= 0) {
            jMin = 0;
        } else {
            jMin = jAtual - 1;
        }
        if (jAtual >= (resolucaoGridX - 1)) {
            jMax = jAtual;
        } else {
            jMax = jAtual + 1;
        }
        if (iAtual <= 0) {
            iMin = 0;
        } else {
            iMin = iAtual - 1;
        }
        if (iAtual >= (resolucaoGridY - 1)) {
            iMax = (resolucaoGridY - 1);
        } else {
            iMax = iAtual + 1;
        }

        //CALCULA A FORÇA DE PRESSAO.
        for (int i = iMin; i <= iMax; i++) {
            for (int j = jMin; j <= jMax; j++) {
                idCell = (i * resolucaoGridX) + j;
                float h2 = h*h;
                float3 forcaPressaoTemp;
                forcaPressaoTemp.x = 0;
                forcaPressaoTemp.y = 0;
                forcaPressaoTemp.z = 0.0f;
                kInicial = offSet[idCell];
                kFinal = offSet[idCell] + count[idCell];

                for (int k = kInicial; k < kFinal; k++) {
                    int indiceParticulaVizinha = values[k];
                    if (indiceParticulaVizinha != indiceParticulaAtual) {
                        float distancia2 = (posicoesParticulasT[indiceParticulaAtual].x - posicoesParticulasT[indiceParticulaVizinha].x) * (posicoesParticulasT[indiceParticulaAtual].x - posicoesParticulasT[indiceParticulaVizinha].x) + (posicoesParticulasT[indiceParticulaAtual].y - posicoesParticulasT[indiceParticulaVizinha].y) * (posicoesParticulasT[indiceParticulaAtual].y - posicoesParticulasT[indiceParticulaVizinha].y);
                        if (distancia2 < h2) {
                            float distancia = sqrtf(distancia2);
                            float3 gradientePressao;
                            if (distancia > 0) {
                                //float mod;
                                /*mod = sqrtf(powf(posicoesParticulasT[indiceParticulaAtual].x - posicoesParticulasT[indiceParticulaVizinha].x, 2) +
                                        powf(posicoesParticulasT[indiceParticulaAtual].y - posicoesParticulasT[indiceParticulaVizinha].y, 2) +
                                        powf(posicoesParticulasT[indiceParticulaAtual].z - posicoesParticulasT[indiceParticulaVizinha].z, 2));*/
                                gradientePressao.x = (posicoesParticulasT[indiceParticulaVizinha].x - posicoesParticulasT[indiceParticulaAtual].x) / distancia;
                                gradientePressao.y = (posicoesParticulasT[indiceParticulaVizinha].y - posicoesParticulasT[indiceParticulaAtual].y) / distancia;
                                //gradientePressao.z = (posicoesParticulasT[indiceParticulaVizinha].z - posicoesParticulasT[indiceParticulaAtual].z) / distancia;
                                float kernelPress = (15 / (3.14 * powf(h, 6))) * (powf((h - distancia), 3));
                                float tempX = ((particulasT[indiceParticulaVizinha].pressao + particulasT[indiceParticulaAtual].pressao) / 2 * particulasT[indiceParticulaVizinha].massaEspecifica);
                                forcaPressaoTemp.x += massa * tempX * gradientePressao.x * kernelPress;
                                float tempY = ((particulasT[indiceParticulaVizinha].pressao + particulasT[indiceParticulaAtual].pressao) / 2 * particulasT[indiceParticulaVizinha].massaEspecifica);
                                forcaPressaoTemp.y += massa * tempY * gradientePressao.y * kernelPress;
                            }
                        }
                    }
                }
                particulasT[indiceParticulaAtual].forcaPressao.x += forcaPressaoTemp.x;
                particulasT[indiceParticulaAtual].forcaPressao.y += forcaPressaoTemp.y;
                particulasT[indiceParticulaAtual].forcaPressao.z += forcaPressaoTemp.z;
            }
        }
        particulasT[indiceParticulaAtual].forcaPressao.x *= -1;
        particulasT[indiceParticulaAtual].forcaPressao.y *= -1;
        particulasT[indiceParticulaAtual].forcaPressao.z *= -1;

    }
}

extern "C" void launch_PressaoKernel(unsigned int blocksPerGrid, unsigned int threadsPerBlock, float3* posicoesParticulasT, float3* forcaPressao, int numeroParticulas, int resolucaoGridX, int resolucaoGridY, unsigned int* values, float deltaXGrid, float deltaYGrid, unsigned int* offSet, unsigned int* count, float h, Particula* particulasT, float massa) {
    // execute the kernel
    PressaoKernel << <blocksPerGrid, threadsPerBlock >> >(posicoesParticulasT, forcaPressao, numeroParticulas, resolucaoGridX, resolucaoGridY, values, deltaXGrid, deltaYGrid, offSet, count, h, particulasT, massa);
}

